package com.wl.dzt.sys.controller.param.shir;

import com.wl.dzt.sys.controller.annotation.Shir;
import com.wl.dzt.sys.controller.param.Param;
import com.wl.dzt.sys.util.SpringUtil;

import java.lang.reflect.Method;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 拦截解析
 * 解析核心控制器拦截器
 */
public class ShirPaser {

    /**
     * <b>解析</b>
     * <p>
     * 根据所传递的表达式对应不同的解析函数<br>
     * bean.before <br>
     * bean.after<br>
     * bean.middle<br>
     * </p>
     *
     * @param exp 表达式
     * @return 构建的shibean
     */
    public static ShirBean paser(String exp) {
        ShirBean bean = new ShirBean();
        String[] binfo = exp.split("[.]");
        String beanName = binfo[0], methodName = binfo[1];
        bean.setBean(SpringUtil.getBean(beanName));
        Method method = null;
        try {
            method = bean.getBean().getClass().getDeclaredMethod(methodName, Param.class);
        } catch (Exception e) {
            try {
                method = bean.getBean().getClass().getDeclaredMethod(methodName, ShirHandler.class);
            } catch (NoSuchMethodException | SecurityException e1) {
                e1.printStackTrace();
            }
        }
        bean.setMethod(method);
        Shir shir = method.getAnnotation(Shir.class);
        if (shir != null) {
            bean.setAfter(shir.value() == ShirTypes.AFTER);
            bean.setBefore(shir.value() == ShirTypes.BEFORE);
            bean.setMiddle(shir.value() == ShirTypes.MIDDLE);
        }
        return bean;
    }
}
